
package com.android.finaltracer.test;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

import android.os.RemoteException;
import android.test.InstrumentationTestCase;

import com.android.finaltracer.model.PositionReport;
import com.android.finaltracer.servercall.RemoteCall;
import com.android.finaltracer.servercall.RemoteCallManager;
import com.android.finaltracer.servercall.SequenceHashmap;

public class WSTestCase extends InstrumentationTestCase {

    public void testUpdateAndGet() throws RemoteException {
        RemoteCall rc = RemoteCallManager.getInstance().getDefaultRemoteCall();
        Map<String, Object> requestMap = new SequenceHashmap<String, Object>();
        requestMap.put("sender", "Leeon");
        requestMap.put("receiver", "David");
        requestMap.put("longitude", 1234);
        requestMap.put("latitude", 4321);

        rc.execute(RemoteCall.ACTION_UPDATE_GPS, requestMap, null);
        requestMap.put("sender", "Kfir");
        requestMap.put("longitude", 111);
        requestMap.put("latitude", 222);
        rc.execute(RemoteCall.ACTION_UPDATE_GPS, requestMap, null);
        requestMap.clear();
        requestMap.put("receiver", "David");

        List<PositionReport> result = rc.execute(RemoteCall.ACTION_GET_GPS,
                requestMap, PositionReport.class);
        assertEquals(2, result.size());
        PositionReport reportLeeon = null;
        PositionReport reportDavid = null;
        for (Iterator<PositionReport> it = result.iterator(); it.hasNext();) {
            PositionReport pr = it.next();
            if ("Leeon".equals(pr.getSender())) {
                reportLeeon = pr;
            } else if ("Kfir".equals(pr.getSender())) {
                reportDavid = pr;
            }
        }
        assertNotNull(reportDavid);
        assertNotNull(reportLeeon);
        assertEquals("David", reportDavid.getReceiver());
        assertEquals("David", reportLeeon.getReceiver());
        assertEquals(1234, reportLeeon.getLongitude());
        assertEquals(111, reportDavid.getLongitude());

        assertEquals(4321, reportLeeon.getLatitude());
        assertEquals(222, reportDavid.getLatitude());

        result = rc.execute(RemoteCall.ACTION_GET_GPS, requestMap,
                PositionReport.class);
        assertEquals(0, result.size());
        /*
         * TestBean updateBean = new TestBean(); updateBean.setUser("Leeon3");
         * updateBean.setLongitude(234.33); updateBean.setLatitude(333.22);
         * RemoteCall rc =
         * RemoteCallManager.getInstance().getDefaultRemoteCall();
         * rc.execute(RemoteCall.ACTION_UPDATE_GPS, updateBean, null); TestBean
         * resultBean = new TestBean(); resultBean.setUser("Leeon3"); TestBean
         * bean = rc.execute(RemoteCall.ACTION_GET_GPS, resultBean,
         * TestBean.class); assertEquals("Leeon3", bean.getUser());
         * assertEquals(234.33, bean.getLongitude()); assertEquals(333.22,
         * bean.getLatitude());
         */
    }

    public void testHeartbeat() throws RemoteException {
        RemoteCall rc = RemoteCallManager.getInstance().getDefaultRemoteCall();
        List result = rc.execute(RemoteCall.ACTION_HEARTBEAT, null, null);
        assertNotNull(result);
        assertEquals(1, result.size());
        assertTrue((Boolean) result.get(0));
    }
}
